<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('modules/Leads/Lead.php');

class SendNotificationLeadLogicHook extends Lead {
	function addCCEmail(&$phpmail, $userid, &$arr){ 
        $report=new User();
		$report->retrieve($userid);
		if (in_array($userid, $arr))
		{
			return;
		}
        $phpmail->AddCC($report->email1, $report->first_name . ' ' . $report->last_name);
       // print_r($arr);
       
        if ($report != null && $report->reports_to_id !='') {  
        	array_push($arr, $userid);
        	$this->addCCEmail($phpmail, $report->reports_to_id, &$arr);
        	
        }
        else
        {
        	return;
        }
    } 
    
 	 function after_save_method($bean, $event, $arguments)
    {
    	global $mod_strings, $app_list_strings;
    	global $current_user;
    	
    	require_once('include/SugarPHPMailer.php');
	     $welcome_msg = new SugarPHPMailer();
	     //Setup template
	     require_once('XTemplate/xtpl.php');
	     $xtpl = new XTemplate('custom/modules/Leads/SendNotificationLead.html');
		 //Send welcome msg to Lead
         $template_name = 'main';
			
		$user_created=new User();
		$user_created->retrieve($bean->created_by);
		//Set RCPT address
	    $welcome_msg->AddAddress($user_created->email1, $user_created->first_name . ' ' . $user_created->last_name);
	    // tanphuqn add code  28/06/2014
	    $arr=array();
	    array_push($arr, $bean->created_by);
	    $this->addCCEmail($welcome_msg, $user_created->reports_to_id, $arr);
	   	// module specific
		$sql = "Select * from leads_audit where parent_id ='{$bean->id}' and date_created = '{$bean->date_modified}'";
		//$sql = "Select * from leads_audit";
		
		$result = $this->db->query($sql,true);
		$is_send_mail = false;
		//Assign values to variables in template
        $xtpl->assign('created_by', $user_created->first_name . ' ' . $user_created->last_name);
		$xtpl->assign('date_modified', $date = date('j-F-Y h:i:s A', strtotime($bean->date_modified)+19800)); // $today is today date
		$xtpl->assign('modified_by', $current_user->first_name . ' ' . $current_user->last_name);
		$xtpl->assign('lead_name', $bean->first_name . ' ' . $bean->last_name);
		$xtpl->assign('status',  $app_list_strings['lead_status_dom'][$bean->status]);
		$xtpl->assign('status_description',$bean->status_description);
		$get_notification_c = $bean->get_notification_c;
		
		while($item   = $this->db->fetchByAssoc($result)){
			foreach ($bean->field_name_map as $value) {
				if($item['field_name'] == $value['name'])
				{
			    	//var_dump($value);
			    	$item['field_name'] = str_replace(":","",$mod_strings[$value['vname']]);
			    	
			    	if($value['name'] == 'assigned_user_id')
					{
						$u=new User();
			    		$item['before_value_string'] = $u->retrieve($item['before_value_string'])->user_name;
			    		$item['after_value_string'] = $u->retrieve($item['after_value_string'])->user_name;
			        }
			    	 else if($value['name'] == 'status')
					{
			    		$item['before_value_string'] = $app_list_strings['lead_status_dom'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['lead_status_dom'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'lead_prospect_status_c')
					{
			    		$item['before_value_string'] = $app_list_strings['opp_saas_sales_stage_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['opp_saas_sales_stage_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'activity_status_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['Prospect_Type_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['Prospect_Type_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'aggregator_name_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['channel_aggregator_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['channel_aggregator_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'assigned_team_id_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['Elastic_boost_options'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['Elastic_boost_options'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'dnd_status_if_no_from_net_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['dnd_status_if_no_from_net_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['dnd_status_if_no_from_net_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'engaged_location_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['EngagedLocation_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['EngagedLocation_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'go_curr_owner_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['go_opp_initiated_by_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['go_opp_initiated_by_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'lead_prod_type_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['Opp_product_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['Opp_product_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'lead_source_description2_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['Lead_Source_Description_list'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['Lead_Source_Description_list'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'lead_source_type_c')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['lead_source_type'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['lead_source_type'][$item['after_value_string']];
			        }
			        else if($value['name'] == 'salutation')
					{
			    		//var_dump($value);
			    		$item['before_value_string'] = $app_list_strings['salutation_dom'][$item['before_value_string']];
			    		$item['after_value_string'] = $app_list_strings['salutation_dom'][$item['after_value_string']];
			        }
			        
			        if($item['before_value_string'] == '')
			        {
			        	$item['before_value_string'] = 'NULL/BLANK';
			        }
					if($item['after_value_string'] == '')
			        {
			        	$item['after_value_string'] = 'NULL/BLANK';
			        }
			    	break;
			    }
			}
		    // assign array data
        	$xtpl->assign('data', $item);
        	$xtpl->parse('main.row');
        	$is_send_mail = true;
		}

		if($is_send_mail && $get_notification_c == 1)
		{
			$xtpl->parse($template_name);
			$welcome_msg->Body = from_html(trim($xtpl->text($template_name))); 
			$welcome_msg->Subject = '[Email Notification] '.$bean->first_name . ' ' . $bean->last_name;
			$welcome_msg->prepForOutbound();
			$welcome_msg->setMailerForSystem();
			$welcome_msg->isHTML(true); // set to true if content has html tags
			$welcome_msg->From = 'info@greytip.com';
			$welcome_msg->FromName = 'GreytHR Partner Team';

			//Send the message, log if error occurs
			if (!$welcome_msg->Send())
			{
			   $GLOBALS['log']->fatal('Error sending e-mail: ' . $welcome_msg->ErrorInfo);
			}  
		}
		else {
			$GLOBALS['log']->fatal('Don\'t change anythings.');
		}
    }
} 


?>